package bean;

import json.person;

import java.io.*;
public class function
{
    //判断是否合法
    public static boolean register_check(String name,String email,String passwd,String passwd2)
    {
        if((!passwd.equals(passwd2))||(name.length()<=0)||(passwd.length()<6)||(email.length()<=0))
        {
            return false;
        }
        return true;
    }
    //判断注册数据是否已经存在
    public static boolean ifExit(BufferedReader input,String name,String email)
    {
        String s1=null,str=null;
        try
        {
            while((s1=input.readLine())!=null)
            {
                str=input.readLine();
                String s2[]=str.split(",");
                if(s2[0].equals(name)||s2[1].equals(email)) {input.close();return false;}
            }
            input.close();
            return true;
        }
        catch(IOException e) {System.out.println("读取文件失败！");return true;}
    }

    public static void writeToUser(BufferedWriter output,String name,String email,String passwd)
    {
        String str=name+","+email+","+passwd+","+null+","+null;
        try
        {
            if(str!=null)
            {
                output.write("###");
                output.newLine();
                output.write(str);
                output.newLine();
            }
            output.close();
        }
        catch(Exception e) {System.out.println("写入文件失败！");}
    }

    //判断登录信息是否存在
    public static User Login_Exit(String name,String passwd)
    {
        User user=null;
        String s1=null,str=null;
        BufferedReader input=null;
        File file=null;
        try {
            file = new File("person.txt");
            input=new BufferedReader(new FileReader(file));

            while((s1=input.readLine())!=null)
            {
                str=input.readLine();
                String s2[]=str.split(",");
                if(s2[0].equals(name)&&s2[2].equals(passwd))
                {
                    user=new User(s2[0],s2[1],s2[2],s2[3],s2[4]);
                    return user;
                }
            }
        }
        catch(IOException e) {System.out.println("登录账户失败！");}
        finally {
            try {
                input.close();
            }catch (Exception ee) {System.out.println("文件关闭失败！");}
            return user;
        }
    }

    public static User LoginDgutUser(person Person)
    {
        BufferedReader input=null;
        File file=null;
        String s1=null;
        String s2=null;
        boolean flag=false;
        User user=null;
        String id=Person.getUsername();
        try {
            file = new File("person.txt");
            input=new BufferedReader(new FileReader(file));
            while((s1=input.readLine())!=null)
            {
                s2=input.readLine();
                if(s2!=null)
                {
                    String s[]=s2.split(",");
                    if((s[4]!=null)&&(s[4].equals(id)))
                    {
                        flag=true;
                        user=new User(s[0],s[1],s[2],s[3],s[4]);
                        return user;
                    }
                }
            }
            if(!flag)
            {
                user=new User("游客",null,"123456",null,Person.getUsername());
                //PackageFunction.writeDgutToUser(user);
                return user;
            }
        }
        catch(IOException e) {System.out.println("登录失败！");}
        finally {
            try {
                input.close();
            }catch (Exception ee) {System.out.println("文件关闭失败！");}
        }
        return user;
    }

    //将第三方莞工账户写入文件
    public static void writeDgutToUser(User user)
    {
        BufferedWriter output=null;
        File file=null;

        String str=user.getUsername()+","+user.getEmail()+","+user.getPasswd()+","+user.getImg()+","+user.getId();
        try
        {
            file=new File("person.txt");
            output=new BufferedWriter(new FileWriter(file,true));
            if(str!=null)
            {
                output.write("###");
                output.newLine();
                output.write(str);
                output.newLine();
            }
        }
        catch(Exception e) {System.out.println("写入失败！");}
        finally {
            try {
                output.close();
            }catch (Exception e2) {System.out.println("文件关闭失败！");}
        }
    }


    public static void UploadImg(User user)
    {
        File file=null;
        BufferedReader input=null;
        BufferedWriter output=null;
        String s1,s2;
        String change;
        StringBuilder sb=new StringBuilder();
        try{
            file=new File("person.txt");
            input=new BufferedReader(new FileReader(file));
            while((s1=input.readLine())!=null)
            {
                s2=input.readLine();
                String str[]=s2.split(",");
                if((user.getUsername().equals(str[0]))) change=user.getUsername()+","+user.getEmail()+","+user.getPasswd()
                        +","+user.getImg()+","+user.getId();
                else change=s2;
                sb.append(s1);sb.append("&");sb.append(change);sb.append("&");
            }
            input.close();
            String str2=sb.toString();
            String ss[]=str2.split("&");
            output=new BufferedWriter(new FileWriter(file));
            for(int i=0;i<ss.length;i++)
            {
                output.write(ss[i]);
                output.newLine();
            }
            output.close();
        }catch(IOException e) {System.out.println("上传头像失败！"+e.getMessage());}
        System.out.println(sb.toString());

    }
}
